package br.com.srv.config;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.Reference;
import javax.naming.StringRefAddr;
import javax.sql.ConnectionPoolDataSource;

import org.apache.commons.dbcp.datasources.PerUserPoolDataSource;

import br.com.srv.exception.FFServiceException;

public class ConfiguracaoJDBCPool {

	public final static String IP_ADDRESS = "localhost";
	public final static String PORTA = "9000";
	public final static String URL_JDBC = "jdbc:mysql://localhost/ffService";
	public final static String USER_JDBC = "root";
	public final static String PWD_JDBC = "root";
	public final static String DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
	public final static String MEU_PROVIDER_URL = "file:///jndi";

	private final static String NAME_JDBC = "jdbc/peruser";

	public ConfiguracaoJDBCPool() throws FFServiceException {

		try {

			System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
					"com.sun.jndi.fscontext.RefFSContextFactory");
			System.setProperty(Context.PROVIDER_URL, MEU_PROVIDER_URL);
			InitialContext ic = new InitialContext();

			// Construct DriverAdapterCPDS reference
			Reference cpdsRef = new Reference(
					"org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS",
					"org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS",
					null);
			cpdsRef.add(new StringRefAddr("driver", DRIVER_CLASS_NAME));
			cpdsRef.add(new StringRefAddr("url", URL_JDBC));
			cpdsRef.add(new StringRefAddr("user", USER_JDBC));
			cpdsRef.add(new StringRefAddr("password", PWD_JDBC));
			ic.rebind("jdbc/cpds", cpdsRef);

			// Construct PerUserPoolDataSource reference
			Reference ref = new Reference(
					"org.apache.commons.dbcp.datasources.PerUserPoolDataSource",
					"org.apache.commons.dbcp.datasources.PerUserPoolDataSourceFactory",
					null);
			ref.add(new StringRefAddr("dataSourceName", "jdbc/cpds"));
			ref.add(new StringRefAddr("defaultMaxActive", "30"));
			ref.add(new StringRefAddr("defaultMaxIdle", "1"));
			ref.add(new StringRefAddr("defaultMaxWait", "1"));
			
			ic.rebind(NAME_JDBC, ref);
			
		} catch (NamingException e) {
			throw new FFServiceException(e, "Erro JNDI");
		}
	}
	
	public static Connection pegarConexao() throws FFServiceException {
		
		Connection conn = null;
		
		try {
			InitialContext ic2 = new InitialContext();
			PerUserPoolDataSource dataSource = (PerUserPoolDataSource) ic2
					.lookup(NAME_JDBC);
			
			ConnectionPoolDataSource connectionPoolDataSource = dataSource.getConnectionPoolDataSource();
			
			if (null == connectionPoolDataSource) {
				conn = dataSource.getConnection();
			} else {
				conn = connectionPoolDataSource.getPooledConnection().getConnection();
			}
			
		} catch (NamingException e) {
			throw new FFServiceException(e, "Erro ao tentar localizar jdbc/basic JNDI");
		} catch (SQLException e) {
			throw new FFServiceException(e, "Erro de SQL");
		} catch (Exception e) {
			throw new FFServiceException(e, "Erro de SQL");
		} finally {
			return conn;
		}
	}

}
